package com.templeix.jfinal.config;

import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.templeix.jfinal.handler.BasePathHandler;
import com.templeix.jfinal.interceptor.UserAuthInterceptor;
import com.templeix.timeaxis.controller.IndexController;
import com.templeix.timeaxis.controller.UserController;
import com.templeix.timeaxis.model.SysMenu;
import com.templeix.timeaxis.model.SysRole;
import com.templeix.timeaxis.model.SysRoleMenu;
import com.templeix.timeaxis.model.SysUser;
import com.templeix.timeaxis.model.SysUserRole;
import com.templeix.timeaxis.model.TADynasty;


public class AppConfig extends JFinalConfig {
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("config.properties");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		//配置模板
		me.setMainRenderFactory(new BeetlRenderFactory());
		//获取GroupTemplate模板，可以设置共享变量操作
		GroupTemplate groupTemplate = BeetlRenderFactory.groupTemplate;
		me.setViewType(ViewType.JSP);
		me.setEncoding("UTF-8");
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
		me.add("/user", UserController.class);	
	}
	
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		Prop prop = PropKit.use("jdbc.properties");
		// 配置C3p0数据库连接池插件
		DruidPlugin druidPlugin = new DruidPlugin(prop.get("jdbcUrl"), prop.get("user"), prop.get("password").trim());
		me.add(druidPlugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin mainArp = new ActiveRecordPlugin("main", druidPlugin);
		me.add(mainArp);
		mainArp.setDialect(new MysqlDialect());
		
		//显示sql
		if (isDevMode()) {
			mainArp.setShowSql(true);
		}
		
		// 表名 -- Class 映射关系
		mainArp.addMapping("t_sys_menu", SysMenu.class);
		mainArp.addMapping("t_sys_role", SysRole.class);
		mainArp.addMapping("t_sys_role_menu", SysRoleMenu.class);
		mainArp.addMapping("t_sys_user", SysUser.class);
		mainArp.addMapping("t_sys_user_role", SysUserRole.class);		
		mainArp.addMapping("t_ta_dynasty", TADynasty.class);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new UserAuthInterceptor());
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		// 全路径获取
		me.add(new BasePathHandler(PropKit.use("config.properties").get("PATH.BASE_PATH")));		
	}
	
	private boolean isDevMode() {
		Prop prop = PropKit.use("config.properties");
		return prop.getBoolean("devMode", false);
	}
	
	/**
	 * 初始化
	 */
	@Override
	public void afterJFinalStart() {
		System.out.println("##################################");
		System.out.println("######### timeaxis启动完成 #########");
		System.out.println("##################################");
	}
}
